package org.hqs.arrangement.service;

import org.hqs.arrangement.mapper.ArrangementMapper;
import org.hqs.arrangement.pojo.Arrangement;
import org.hqs.feign.clients.ClinicClient;
import org.hqs.feign.clients.DoctorClient;
import org.hqs.feign.pojo.Clinic;
import org.hqs.feign.pojo.Doctor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ArrangementService {

    @Autowired
    private ArrangementMapper arrangementMapper;

    @Autowired
    private DoctorClient doctorClient;

    @Autowired
    private ClinicClient clinicClient;

    public List<Arrangement> getAllArrangements() {
        List<Arrangement> arrangements = arrangementMapper.getAllArrangements();
        for (int i = 0; i < arrangements.size(); i++) {
            Arrangement arrangement = arrangements.get(i);
            Doctor doctor = doctorClient.queryDoctorById(Long.valueOf(arrangement.getDoctorId())).getData();
            Clinic clinic = clinicClient.queryClinicById(Long.valueOf(arrangement.getClinicId())).getData();
            arrangement.setDoctor(doctor);
            arrangement.setClinic(clinic);

            arrangements.set(i, arrangement);
        }
        return arrangements;
    }


    public Arrangement getArrangementById(int id) {
        Arrangement arrangement = arrangementMapper.getArrangementById(id);

        // 找出doctor和clinic
        Doctor doctor = doctorClient.queryDoctorById(Long.valueOf(arrangement.getDoctorId())).getData();
        Clinic clinic = clinicClient.queryClinicById(Long.valueOf(arrangement.getClinicId())).getData();

        arrangement.setDoctor(doctor);
        arrangement.setClinic(clinic);

        return arrangement;
    }


    public int insertArrangement(Arrangement arrangement) {
        return arrangementMapper.insertArrangement(arrangement);
    }


    public int updateArrangement(Arrangement arrangement) {
        return arrangementMapper.updateArrangement(arrangement);
    }


    public int deleteArrangementById(int id) {
        return arrangementMapper.deleteArrangementById(id);
    }

    public List<Arrangement> findArrangementByClinicId(int id) {
        List<Arrangement> arrangements = arrangementMapper.findArrangementByClinicId(id);
        for (int i = 0; i < arrangements.size(); i++) {
            Arrangement arrangement = arrangements.get(i);
            Doctor doctor = doctorClient.queryDoctorById(Long.valueOf(arrangement.getDoctorId())).getData();
            Clinic clinic = clinicClient.queryClinicById(Long.valueOf(arrangement.getClinicId())).getData();
            arrangement.setDoctor(doctor);
            arrangement.setClinic(clinic);

            arrangements.set(i, arrangement);
        }
        return arrangements;
    }
}
